package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysTypeDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysTypeMapper;
import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.service.ISysTypeService;

/**
 * 分类Service业务层处理
 *
 * @author jjp
 * @date 2020-04-09
 */
@Service
public class SysTypeServiceImpl implements ISysTypeService {
    @Autowired
    private SysTypeMapper sysTypeMapper;

    /**
     * 查询分类
     *
     * @param typeId 分类ID
     * @return 分类
     */
    @Override
    public SysType selectSysTypeById(Long typeId) {
        return sysTypeMapper.selectSysTypeById(typeId);
    }

    /**
     * 查询分类列表
     *
     * @param sysType 分类
     * @return 分类
     */
    @Override
    public List<SysType> selectSysTypeList(SysType sysType) {
        return sysTypeMapper.selectSysTypeList(sysType);
    }

    @Override
    public List<SysType> getTypeByCityId(SysType sysType) {
        return sysTypeMapper.getTypeByCityId(sysType);
    }

    /**
     * 新增分类
     *
     * @param sysType 分类
     * @return 结果
     */
    @Override
    public int insertSysType(SysType sysType) {
        sysType.setCreateTime(DateUtils.getNowDate());
        int i = sysTypeMapper.insertSysType(sysType);
        SysType info = sysTypeMapper.selectSysTypeById(sysType.getParentTypeId());
        if (info == null) {
            return i;
        }
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
            throw new CustomException("类型停用，不允许新增");
        }
        String ancestors = null;
        if (info.getAncestors() == null) {
            ancestors = "";
        } else {
            ancestors = info.getAncestors() + ",";
        }
        sysType.setAncestors(ancestors + sysType.getParentTypeId());
        return sysTypeMapper.updateSysTypeAncestors(sysType);
    }

    /**
     * 修改分类
     *
     * @param sysType 分类
     * @return 结果
     */
    @Override
    public int updateSysType(SysType sysType) {
        sysType.setUpdateTime(DateUtils.getNowDate());
        return sysTypeMapper.updateSysType(sysType);
    }

    @Override
    public int updateSysTypes(SysType sysType) {
        return sysTypeMapper.updateSysTypes(sysType);
    }

    /**
     * 批量删除分类
     *
     * @param typeIds 需要删除的分类ID
     * @return 结果
     */
    @Override
    public int deleteSysTypeByIds(Long[] typeIds) {

        return sysTypeMapper.deleteSysTypeByIds(typeIds);
    }

    /**
     * 删除分类信息
     *
     * @param typeId 分类ID
     * @return 结果
     */
    @Override
    public int deleteSysTypeById(Long typeId) {

        return sysTypeMapper.deleteSysTypeById(typeId);
    }


    /**
     * 构建前端所需要树结构
     *
     * @param types 类型列表
     * @return 树结构列表
     */
    @Override
    public List<SysType> buildTypeTree(List<SysType> types) {
        List<Long> typeIds = new ArrayList<>();
        for (SysType sysType : types) {
            typeIds.add(sysType.getTypeId());
        }

        List<Long> parentTypeIds = new ArrayList<>();
        for (SysType sysType : types) {
            if (!typeIds.contains(sysType.getParentTypeId())) {
                parentTypeIds.add(sysType.getTypeId());
            }
        }
        List<SysType> returnList = new ArrayList<SysType>();
        for (Iterator<SysType> iterator = types.iterator(); iterator.hasNext(); ) {
            SysType t = (SysType) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (parentTypeIds.contains(t.getTypeId())) {
                recursionFn(types, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = types;
        }
        return returnList;
    }

    @Override
    public SysType queryDetail(Long typeId) {
        return sysTypeMapper.querydetail(typeId);
    }

    @Override
    public List<SysType> queryParentName() {
        return sysTypeMapper.queryParentName();
    }

    @Override
    public List<SysType> queryChirdrenName(SysType sysType) {
        return sysTypeMapper.queryChirdrenName(sysType);
    }

    @Override
    public int updateSysTypeDetailId(SysType sysType) {
        return sysTypeMapper.updateSysTypeDetailId(sysType);
    }

    @Override
    public List<SysType> queryAllType(SysType sysType) {
        return sysTypeMapper.queryAllType(sysType);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysType> list, SysType t) {
        // 得到子节点列表
        List<SysType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysType tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysType> it = childList.iterator();
                while (it.hasNext()) {
                    SysType n = (SysType) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysType> getChildList(List<SysType> list, SysType t) {
        List<SysType> tlist = new ArrayList<SysType>();
        Iterator<SysType> it = list.iterator();
        while (it.hasNext()) {
            SysType n = (SysType) it.next();
            if (null != n.getParentTypeId() && (n.getParentTypeId().longValue() == t.getTypeId().longValue())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysType> list, SysType t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
